using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using ShopForWeb.Dtos.BasicView;
using ShopForWeb.Models.Basic;
using ShopForWeb.Models.DbContexts;

namespace ShopForWeb.Services
{
  public class UserServices
  {
    private ShopDbContext _Db;
    private IMapper _Mapper;
    public UserServices(ShopDbContext db, IMapper mapper)
    {
      this._Db = db;
      this._Mapper = mapper;
    }
    public async Task<int> AddNewAsync(User user)
    {
      var entity = _Db.Entry<User>(user);
      entity.State = EntityState.Added;
      return await _Db.SaveChangesAsync();
    }
    public async Task<UserView?> GetUserByLoginAsync(string phone, string password)
    {
      var query = _Db.UserSet.Where(u => u.Phone == phone && u.Password == password);
      var obj = await query.FirstOrDefaultAsync<User>();
      return obj == null ? null : _Mapper.Map<UserView>(obj);
    }
  }
}
